package cn.middle.swap.portal;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.middle.common.result.ApiResponse;
import com.middle.common.result.Page;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.middle.swap.entity.App;
import cn.middle.swap.service.AppService;

/**
 * 授权服务
 * @author KING
 *
 */
@Controller
@RequestMapping("/auth")
public class AuthController {
	
	@Autowired
	private AppService appService;
	
	private final static String PREFIX = "swap/auth/";
	
	@SaCheckPermission("auth:list")
	@GetMapping("/list")
	public String list(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer limit, String name, ModelMap modelMap) {
		Page<App> apps = appService.listConditions(pageNum, limit, name);
		modelMap.put("pageInfo", apps);
		modelMap.put("name", name);
		return PREFIX + "list";
	}
	
	@SaCheckPermission("auth:add")
	@GetMapping("/add")
	public String add() {
		return PREFIX + "add";
	}
	
	@SaCheckPermission("auth:add")
	@PostMapping("/add")
	@ResponseBody
	public ApiResponse<Boolean> add(@Valid App app){
		this.appService.save(app);
		return ApiResponse.successWithData(true);
	}

}
